package com.zyj.assets.controller;

import java.util.HashMap;
import java.util.List;

import com.zyj.common.annotation.Log;
import com.zyj.common.config.ZyjConfig;
import com.zyj.common.constant.Constants;
import com.zyj.common.core.controller.BaseController;
import com.zyj.common.core.domain.AjaxResult;
import com.zyj.common.core.page.TableDataInfo;
import com.zyj.common.core.text.Convert;
import com.zyj.common.enums.BusinessType;
import com.zyj.common.utils.poi.ExcelUtil;
import com.zyj.system.service.ISysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zyj.assets.domain.AssetsInfo;
import com.zyj.assets.service.IAssetsInfoService;

/**
 * 资产Controller
 * 
 * @author zhuyujie
 * @date 2022-05-09
 */
@Controller
@RequestMapping("/assets/assets")
public class AssetsInfoController extends BaseController
{
    private String prefix = "assets/assets";

    @Autowired
    private IAssetsInfoService assetsInfoService;

    @Autowired
    private ISysConfigService configService;

    @GetMapping()
    public String assets()
    {
        return prefix + "/assets";
    }

    /**
     * 查询资产列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AssetsInfo assetsInfo)
    {
        startPage();
        List<AssetsInfo> list = assetsInfoService.selectAssetsInfoList(assetsInfo);
        return getDataTable(list);
    }

    /**
     * 查询资产列表-非废弃
     */
    @PostMapping("/listWithOutScrap")
    @ResponseBody
    public TableDataInfo listWithOutScrap(AssetsInfo assetsInfo)
    {
        startPage();
        List<AssetsInfo> list = assetsInfoService.selectAssetsInfoWithOutScrapList(assetsInfo);
        return getDataTable(list);
    }

    /**
     * 导出资产列表
     */
    @Log(title = "导出资产列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AssetsInfo assetsInfo)
    {
        List<AssetsInfo> list = assetsInfoService.selectAssetsInfoList(assetsInfo);
        ExcelUtil<AssetsInfo> util = new ExcelUtil<AssetsInfo>(AssetsInfo.class);
        return util.exportExcel(list, "资产数据");
    }

    /**
     * 新增资产
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("scannerEnabled", Convert.toBool(configService.selectConfigByKey("sys.assets.scannerEnabled"), true));
        return prefix + "/add";
    }

    /**
     * 新增保存资产
     */
    @Log(title = "资产", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AssetsInfo assetsInfo)
    {
        return toAjax(assetsInfoService.insertAssetsInfo(assetsInfo));
    }

    /**
     * 修改资产
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        AssetsInfo assetsInfo = assetsInfoService.selectAssetsInfoById(id);
        mmap.put("scannerEnabled", Convert.toBool(configService.selectConfigByKey("sys.assets.scannerEnabled"), true));
        mmap.put("assetsInfo", assetsInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产
     */
    @Log(title = "资产", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AssetsInfo assetsInfo)
    {
        return toAjax(assetsInfoService.updateAssetsInfo(assetsInfo));
    }

    /**
     * 删除资产
     */
    @Log(title = "资产", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(assetsInfoService.deleteAssetsInfoByIds(ids));
    }

    /**
     * 生成获取扫描key
     */
    @GetMapping("/makeSnKey/{snKey}")
    @ResponseBody
    public AjaxResult makeSnKey(@PathVariable("snKey") String snKey)
    {
        Constants.makeSnKey = new HashMap<>();
        Constants.makeSnKey.put(snKey,"");
        System.out.println(Constants.makeSnKey);
        return AjaxResult.success();
    }

    /**
     * 生成获取扫描key
     */
    @GetMapping("/getSnKey/{snKey}")
    @ResponseBody
    public AjaxResult getSnKey(@PathVariable("snKey") String snKey)
    {
        System.out.println("getSnKey="+Constants.makeSnKey);
        if(Constants.makeSnKey.get(snKey)!=null && !"".equals(Constants.makeSnKey.get(snKey))){
            Object sn = Constants.makeSnKey.get(snKey);
            Constants.makeSnKey = new HashMap<>();
            System.out.println("sn="+sn);
            return AjaxResult.success(sn);
        }else{
            return AjaxResult.success();
        }

    }
}
